<?php


namespace App\Http\Controllers\Backend\Advertisement;
use App\ConstDir\ErrorConst;
use App\Http\Controllers\Backend\BaseController;
use App\Repository\Models\AdvertisementPosition;
use App\Repository\Models\ArticleCategory;
use App\Repository\Repositories\Interfaces\AdvertisementRepository;
use App\Repository\Repositories\Interfaces\AdvertisementPositionRepository;
use Illuminate\Http\Request;


class AdvertisingListController  extends BaseController
{
    protected $advertisement;
    protected $advertisingposition;
    protected $request;
    public function __construct(AdvertisementRepository $advertisement,Request $request,AdvertisementPositionRepository $advertisingposition) {
        $this->advertisement=$advertisement;
        $this->request=$request;
        $this->advertisingposition=$advertisingposition;
    }

    /**
     * 广告列表
     * 分页
     * */
    public function index(){

        $data=$this->advertisement->with(['AdvertisementPosition','ProductCategories'])->orderBy('id','desc')->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE, $data, true);
    }

    /**
     * 广告详情
     * @param $id
     * @return \Illuminate\Support\Collection
     */
    public function show($id){
        $data=$this->advertisement->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE, $data);

    }
    /**
     * 广告添加
     *
     * */
    public function store(){
        $data=$this->request->all();
        $ret=$this->advertisement->create($data);
        return $this->success(ErrorConst::SUCCESS_CODE, $data);

    }
    /**
     * 广告修改
     *
     * */
    public function update($id){
        $data=$this->request->all();
        $ret=$this->advertisement->update($data,$id);
        return $this->success(ErrorConst::SUCCESS_CODE, $ret);

    }

    /**
     * 设置前台展示
     *
     * */
    public function audit($id){
        $data=$this->request->all(['audit']);
        $ret=$this->advertisement->update($data,$id);
        return $this->success(ErrorConst::SUCCESS_CODE, $ret);
    }


    /**
     * 广告位置删除
     *
     * */
    public function delete($id){
        $ret = $this->advertisement->delete($id);

        return $this->success(ErrorConst::SUCCESS_CODE, ErrorConst::SUCCESS_CODE_MSG);
    }


    /**
     * 获取广告分类
     */
    public function category(){
        $ret=AdvertisementPosition::query()->where('audit','=',1)->get()->toArray();
        return $this->success(ErrorConst::SUCCESS_CODE, $ret);

    }


}